package com.maomao.crawler;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class crawler {

    public static void main(String[] args) throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless");
//        options.addArguments("--proxy-server=http://202.20.16.82:10152");
        WebDriver driver = new ChromeDriver(options);

        /*String url = "https://www.amazon.com/s?i=baby-products-intl-ship&bbn=16225005011&rh=n%3A16225005011%2Cn%3A166887011&qid=1603425198&ref=sr_pg_2";
        driver.get(url);
        List<WebElement> lis = driver.findElements(By.cssSelector(".a-link-normal.s-no-outline"));
        System.out.println(lis.size());
        for (WebElement li:lis) {
            String link = li.getAttribute("href");
            System.out.println(link);
        }*/

        String productUrl = "https://www.amazon.com/-/zh/dp/B01BH9YR4W/ref=sr_1_1?dchild=1&qid=1603425198&s=baby-products-intl-ship&sr=1-1";
        driver.get(productUrl);
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//        System.out.println(driver.getPageSource());
        // 产品图片
        WebElement imgTagWrapperId = driver.findElement(By.id("imgTagWrapperId"));
        WebElement productImgE = imgTagWrapperId.findElement(By.tagName("img"));
        String productImg = productImgE.getAttribute("src");
        System.out.println(productImg);
        // 标题
        WebElement productTitleE = driver.findElement(By.id("productTitle"));
        String productTitle = productTitleE.getText();
        System.out.println(productTitle);
        // 星级别
        WebElement acrCustomerReviewText = driver.findElement(By.id("acrCustomerReviewText"));
        String customerReview = acrCustomerReviewText.getText();
        System.out.println(customerReview);

        WebElement sizeMediumE = driver.findElement(By.cssSelector(".a-size-medium.a-color-price"));
        String sizeMedium = sizeMediumE.getText();
        System.out.println(sizeMedium);


    }
}
